﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace QuanLyPhongKham
{
    public partial class frmDmthuoc : BaseUserControl
    {
        private DAL.AccessData m;
        private DataTable dthoatchat = new DataTable();
        private long l_id = 0, l_iddvt = 0, l_idhc = 0, l_idquycach = 0;
        private string s_mmyy = "";
        private int i_userid;
        private DataTable dt = new DataTable();
        private DataTable dthc = new DataTable();
        private bool bNew = false;

        public frmDmthuoc(DAL.AccessData acc, int userid,string mmyy)
        {
            this.m = acc;
            i_userid = userid;
            this.s_mmyy = mmyy;
            InitializeComponent();
            this.Title = "Thuốc - Vật tư";
        }

        private void frmDmthuoc_SizeChanged(object sender, EventArgs e)
        {
            //bNut.Location = new Point((this.Width - 628) / 2 + 1, 124);
        }

        private void butKetthuc_Click(object sender, EventArgs e)
        {
            //this.Close();
        }

        private void mathuoc_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SendKeys.Send("{Tab}");
            }
        }

        private void tenthuoc_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SendKeys.Send("{Tab}");
            }
        }

        private void hamluong_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SendKeys.Send("{Tab}");
            }
        }

        private void dvt_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Down || e.KeyCode == Keys.Up) listdvt.Focus();
            else if (e.KeyCode == Keys.Enter)
            {
                if (listdvt.Visible) listdvt.Focus();
                else SendKeys.Send("{Tab}{Home}");
            }
        }

        private void donggoi_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Down || e.KeyCode == Keys.Up) listdongoi.Focus();
            else if (e.KeyCode == Keys.Enter)
            {
                if (listdongoi.Visible) listdongoi.Focus();
                else SendKeys.Send("{Tab}{Home}");
            }
        }

        private void hoatchat_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Down || e.KeyCode == Keys.Up) listhoatchat.Focus();
            else if (e.KeyCode == Keys.Enter)
            {
                if (listhoatchat.Visible) listhoatchat.Focus();
                else SendKeys.Send("{Tab}{Home}");
            }
        }

        private void butChonNhom_Click(object sender, EventArgs e)
        {
            frmDmnhom f = new frmDmnhom(m, "d_dmnhom", "id", "ten", "Mã nhóm", "Tên nhóm", 2, "Danh mục nhóm","manhom");
            //f.ShowDialog();
            load_nhom();
        }

        private void butChonHang_Click(object sender, EventArgs e)
        {
            frmDmnhom f = new frmDmnhom(m, "d_dmhang", "id", "ten", "Mã hãng", "Tên hãng", 3, "Danh mục hãng sản xuất","mahang");
            //f.ShowDialog();
            load_hangsx();
        }

        private void butChonNuoc_Click(object sender, EventArgs e)
        {
            frmDmnhom f = new frmDmnhom(m, "d_dmnuoc", "id", "ten", "Mã nước", "Tên nước", 4, "Danh mục nước sản xuất","manuoc");
            //f.ShowDialog();
            load_nuocsx();
        }

        private void butChonCongty_Click(object sender, EventArgs e)
        {
            frmDmnhom f = new frmDmnhom(m, "d_dmnx", "id", "ten", "Mã NCC", "Tên NCC", 5, "Danh mục công ty sản xuất","madv");
            //f.ShowDialog();
            load_congty();
        }

        //private void butLuu_Click(object sender, EventArgs e)
        //{
        //    if (!kiemtra()) return;
        //    if (mathuoc.Text == "") mathuoc.Text = m.getMabd("d_dmbd", tenthuoc.Text, int.Parse(manhom.Text));
        //    l_id = (bNew) ? m.get_capid_dmbd : l_id;
        //    if (bNew)
        //    {
        //        if (madvt.Text.Trim() == "")
        //        {
        //            l_iddvt = m.get_capid_dmdvt;
        //            m.upd_d_cachdung_dvt_quycach(l_iddvt, dvt.Text, "d_dmdvt");
        //        }
        //        if (madonggoi.Text.Trim() == "")
        //        {
        //            l_idquycach = m.get_capid_dmquycach;
        //            m.upd_d_cachdung_dvt_quycach(l_idquycach, quycach.Text, "d_dmquycach");
        //        }
        //        if (mahc.Text.Trim() == "")
        //        {
        //            l_idhc = m.get_capid_dmhoatchat;
        //            mahc.Text=m.getMahc(hoatchat.Text);
        //            m.upd_d_hoatchat(l_idhc, mahc.Text ,hoatchat.Text, chkNotuse.Checked ? 1 : 0);
        //        }
        //    }
        //    m.upd_d_dmbd(l_id, mathuoc.Text, tenthuoc.Text, dvt.Text, hamluong.Text, quycach.Text, duongdung.Text, int.Parse(manhom.Text), int.Parse(mahang.Text), int.Parse(manuoc.Text), hoatchat.Text, mahc.Text, int.Parse(macongty.Text), i_userid, (chkNotuse.Checked ? 1 : 0));
        //    load_grid();
        //    reft_text();
        //    ena_button(true);
        //    ena_object(false);
        //    bNew = false;
        //}
        
        private bool kiemtra()
        {
            if (tenthuoc.Text == "")
            {
                MessageBox.Show("Nhập tên thuốc!", DAL.AccessData.Msg); tenthuoc.Focus(); return false; 
            }
            if (dvt.Text == "")
            {
                MessageBox.Show("Nhập đơn vị tính!", DAL.AccessData.Msg); dvt.Focus(); return false; 
            }
            if (dvt.Text == "")
            {
                MessageBox.Show("Nhập đơn vị tính!", DAL.AccessData.Msg); dvt.Focus(); return false;
            }
            if (manhom.Text == "")
            {
                MessageBox.Show("Tên nhóm không hợp lê! Vui lòng chọn trong danh mục có sẵn!", DAL.AccessData.Msg); nhom.Focus(); return false;
            }
            if (manuoc.Text == "")
            {
                MessageBox.Show("Tên nước sản xuất không hợp lê! Vui lòng chọn trong danh mục có sẵn!", DAL.AccessData.Msg); nuocsx.Focus(); return false;
            }
            if (mahang.Text == "")
            {
                MessageBox.Show("Tên hãng sản xuất không hợp lê! Vui lòng chọn trong danh mục có sẵn!", DAL.AccessData.Msg); hangsx.Focus(); return false;
            }
            if (macongty.Text == "")
            {
                MessageBox.Show("Tên công ty không hợp lê! Vui lòng chọn trong danh mục có sẵn!", DAL.AccessData.Msg); congty.Focus(); return false;
            }
            return true;
        }
        private void hoatchat_TextChanged(object sender, EventArgs e)
        {
            if (this.ActiveControl == hoatchat)
            {
                try
                {
                    Filt_hoatchat(hoatchat.Text);
                    listhoatchat.BrowseToBtdkp(hoatchat, mahc, nhom);
                }
                catch { }
            }
        }
        private void Filt_hoatchat(string ten)
        {
            try
            {
                CurrencyManager cm = (CurrencyManager)BindingContext[listhoatchat.DataSource];
                DataView dv = (DataView)cm.List;
                dv.RowFilter = "tenhc like '%" + ten.Trim() + "%'";
            }
            catch { }
        }

        private void frmDmthuoc_Load(object sender, EventArgs e)
        {
            listhoatchat.DisplayMember = "ID";
            listhoatchat.ValueMember = "TENHC";

            listdongoi.DisplayMember = "ID";
            listdongoi.ValueMember = "TEN";

            listdvt.DisplayMember = "ID";
            listdvt.ValueMember = "TEN";

            listnhom.DisplayMember = "ID";
            listnhom.ValueMember = "TEN";

            listhang.DisplayMember = "ID";
            listhang.ValueMember = "TEN";

            listnuocsx.DisplayMember = "ID";
            listnuocsx.ValueMember = "TEN";

            listcongty.DisplayMember = "ID";
            listcongty.ValueMember = "TEN";

            load_danhmuc();
            load_hangsx();
            load_nhom();
            load_nuocsx();
            load_congty();
            load_hoatchat(); 
            load_grid();
            AddGridTableStyle();
            ena_object(false);
            ena_button(true);
            reft_text();
        }
        private void load_danhmuc()
        {
            
            listdongoi.DataSource = m.get_data("select * from " + m.user + ".d_dmquycach order by ten").Tables[0]; ;
            listdvt.DataSource = m.get_data("select * from " + m.user + ".d_dmdvt order by ten").Tables[0]; ;
        }
        private void load_hangsx()
        {
            listhang.DataSource = m.get_data("select * from " + m.user + ".d_dmhang order by ten").Tables[0]; ;
        }
        private void load_nuocsx()
        {
            listnuocsx.DataSource = m.get_data("select * from " + m.user + ".d_dmnuoc order by ten").Tables[0]; ;
        }
        private void load_nhom()
        {
            listnhom.DataSource = m.get_data("select * from " + m.user + ".d_dmnhom order by ten").Tables[0]; ;
        }
        private void load_congty()
        {
            listcongty.DataSource = m.get_data("select * from " + m.user + ".d_dmnx order by ten").Tables[0]; ;
        }
        private void load_hoatchat()
        {
            dthc = m.get_data("select id,tenhc,mahc from " + m.user + ".d_dmhoatchat where hide=0 order by tenhc").Tables[0];
            listhoatchat.DataSource = dthc;
        }
        private void nhom_TextChanged(object sender, EventArgs e)
        {
            if (this.ActiveControl == nhom)
            {
                try
                {
                    Filt_nhom(nhom.Text);
                    listnhom.BrowseToBtdkp(nhom, manhom, hangsx);
                }
                catch { }
            }
        }
        private void Filt_nhom(string ten)
        {
            try
            {
                CurrencyManager cm = (CurrencyManager)BindingContext[listnhom.DataSource];
                DataView dv = (DataView)cm.List;
                dv.RowFilter = "ten like '%" + ten.Trim() + "%'";
            }
            catch { }
        }

        private void nhom_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Down || e.KeyCode == Keys.Up) listnhom.Focus();
            else if (e.KeyCode == Keys.Enter)
            {
                if (listnhom.Visible) listnhom.Focus();
                else SendKeys.Send("{Tab}{Home}");
            }
        }

        private void hangsx_TextChanged(object sender, EventArgs e)
        {
            if (this.ActiveControl == hangsx)
            {
                try
                {
                    Filt_hang(hangsx.Text);
                    listhang.BrowseToBtdkp(hangsx, mahang, nuocsx);
                }
                catch { }
            }
        }

        private void Filt_hang(string ten)
        {
            try
            {
                CurrencyManager cm = (CurrencyManager)BindingContext[listhang.DataSource];
                DataView dv = (DataView)cm.List;
                dv.RowFilter = "ten like '%" + ten.Trim() + "%'";
            }
            catch { }
        }

        private void hangsx_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Down || e.KeyCode == Keys.Up) listhang.Focus();
            else if (e.KeyCode == Keys.Enter)
            {
                if (listhang.Visible) listhang.Focus();
                else SendKeys.Send("{Tab}{Home}");
            }
        }

        private void nuocsx_TextChanged(object sender, EventArgs e)
        {
            if (this.ActiveControl == nuocsx)
            {
                try
                {
                    Filt_nuoc(nuocsx.Text);
                    listnuocsx.BrowseToBtdkp(nuocsx, manuoc, congty);
                }
                catch { }
            }
        }
        private void Filt_nuoc(string ten)
        {
            try
            {
                CurrencyManager cm = (CurrencyManager)BindingContext[listnuocsx.DataSource];
                DataView dv = (DataView)cm.List;
                dv.RowFilter = "ten like '%" + ten.Trim() + "%'";
            }
            catch { }
        }

        private void nuocsx_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Down || e.KeyCode == Keys.Up) listnuocsx.Focus();
            else if (e.KeyCode == Keys.Enter)
            {
                if (listnuocsx.Visible) listnuocsx.Focus();
                else SendKeys.Send("{Tab}{Home}");
            }
        }

        private void congty_TextChanged(object sender, EventArgs e)
        {
            if (this.ActiveControl == congty)
            {
                try
                {
                    Filt_congty(congty.Text);
                    listcongty.BrowseToBtdkp(congty, macongty, butLuu);
                }
                catch { }
            }
        }
        private void Filt_congty(string ten)
        {
            try
            {
                CurrencyManager cm = (CurrencyManager)BindingContext[listcongty.DataSource];
                DataView dv = (DataView)cm.List;
                dv.RowFilter = "ten like '%" + ten.Trim() + "%'";
            }
            catch { }
        }

        private void congty_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Down || e.KeyCode == Keys.Up) listcongty.Focus();
            else if (e.KeyCode == Keys.Enter)
            {
                if (listcongty.Visible) listcongty.Focus();
                else SendKeys.Send("{Tab}{Home}");
            }
        }

        private void dvt_TextChanged(object sender, EventArgs e)
        {
            if (this.ActiveControl == dvt)
            {
                try
                {
                    Filt_dvt(dvt.Text);
                    listdvt.BrowseToBtdkp(dvt, madvt, duongdung);
                }
                catch { madvt.Text = ""; }
            }
        }
        private void Filt_dvt(string ten)
        {
            try
            {
                CurrencyManager cm = (CurrencyManager)BindingContext[listdvt.DataSource];
                DataView dv = (DataView)cm.List;
                dv.RowFilter = "ten like '%" + ten.Trim() + "%'";
            }
            catch { madvt.Text = ""; }
        }

        private void donggoi_TextChanged(object sender, EventArgs e)
        {
            if (this.ActiveControl == quycach)
            {
                try
                {
                    Filt_donggoi(quycach.Text);
                    listdongoi.BrowseToBtdkp(quycach, madonggoi, dvt);
                }
                catch { madonggoi.Text = ""; }
            }
        }
        private void Filt_donggoi(string ten)
        {
            try
            {
                CurrencyManager cm = (CurrencyManager)BindingContext[listdongoi.DataSource];
                DataView dv = (DataView)cm.List;
                dv.RowFilter = "ten like '%" + ten.Trim() + "%'";
            }
            catch { madonggoi.Text = ""; }
        }

        private void dvt_Validated(object sender, EventArgs e)
        {
            hide_list(listdvt);
        }
        private void hide_list(LibList.List list)
        {
            if (!list.Focused) list.Hide();
        }

        private void donggoi_Validated(object sender, EventArgs e)
        {
            hide_list(listdongoi);
        }

        private void hoatchat_Validated(object sender, EventArgs e)
        {
            hide_list(listhoatchat);
        }

        private void nhom_Validated(object sender, EventArgs e)
        {
            hide_list(listnhom);
        }

        private void hangsx_Validated(object sender, EventArgs e)
        {
            hide_list(listhang);
        }

        private void nuocsx_Validated(object sender, EventArgs e)
        {
            hide_list(listnuocsx);
        }

        private void congty_Validated(object sender, EventArgs e)
        {
            hide_list(listcongty);
        }
        private void load_grid()
        {
            var ds = m.get_data("select a.*, b.ten as tennhom, c.ten as tenhang, d.ten as tennuoc, e.ten as tencongty from " + m.user + ".d_dmbd a," + m.user + ".d_dmnhom b, " + m.user + ".d_dmhang c," + m.user + ".d_dmnuoc d, " + m.user + ".d_dmnx e where a.manhom=b.id and a.mahang=c.id and a.manuoc=d.id and a.madv=e.id(+)");
            if (ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];
                dataGrid1.DataSource = dt;
                gvDanhSach.DataSource = dt;
            }
        }
        private void load_detail()
        {
            DataRow r = m.getrowbyid(dt, "id=" + l_id);
            if (r != null)
            {
                mathuoc.Text = r["ma"].ToString();
                tenthuoc.Text = r["ten"].ToString();
                hamluong.Text = r["hamluong"].ToString();
                dvt.Text = r["dang"].ToString();
                duongdung.Text = r["duongdung"].ToString();
                quycach.Text = r["quycach"].ToString();
                hoatchat.Text = r["tenhc"].ToString();
                nhom.Text = r["tennhom"].ToString();
                hangsx.Text = r["tenhang"].ToString();
                nuocsx.Text = r["tennuoc"].ToString();
                congty.Text = r["tencongty"].ToString();
                manhom.Text = r["manhom"].ToString();
                mahang.Text = r["mahang"].ToString();
                manuoc.Text = r["manuoc"].ToString();
                macongty.Text = r["madv"].ToString();
                chkNotuse.Checked = r["hide"].ToString() == "1";
            }
        }
        private void AddGridTableStyle()
        {
            DataGridColoredTextBoxColumn TextCol;
            delegateGetColorRowCol de = new delegateGetColorRowCol(MyGetColorRowCol);
            DataGridTableStyle ts = new DataGridTableStyle();
            ts.MappingName = dt.TableName;
            ts.AlternatingBackColor = Color.Linen;
            ts.BackColor = Color.GhostWhite;
            ts.ForeColor = Color.MidnightBlue;
            ts.GridLineColor = SystemColors.Control;
            ts.HeaderBackColor = SystemColors.Control;
            ts.HeaderForeColor = Color.Navy;
            ts.SelectionBackColor = Color.Teal;
            ts.SelectionForeColor = Color.White;
            ts.ReadOnly = true;
            ts.RowHeaderWidth = 5;

            TextCol = new DataGridColoredTextBoxColumn(de, 0);
            TextCol.MappingName = "id";
            TextCol.HeaderText = "";
            TextCol.Width = 0;
            ts.GridColumnStyles.Add(TextCol);
            dataGrid1.TableStyles.Add(ts);

            TextCol = new DataGridColoredTextBoxColumn(de, 0);
            TextCol.MappingName = "ma";
            TextCol.HeaderText = "Mã thuốc";
            TextCol.Width = 80;
            ts.GridColumnStyles.Add(TextCol);
            dataGrid1.TableStyles.Add(ts);

            TextCol = new DataGridColoredTextBoxColumn(de, 1);
            TextCol.MappingName = "tenhc";
            TextCol.HeaderText = "Tên hoạt chất";
            TextCol.Width = 180;
            ts.GridColumnStyles.Add(TextCol);
            dataGrid1.TableStyles.Add(ts);

            TextCol = new DataGridColoredTextBoxColumn(de, 2);
            TextCol.MappingName = "ten";
            TextCol.HeaderText = "Tên thuốc -  hàm lượng";
            TextCol.Width = 300;
            ts.GridColumnStyles.Add(TextCol);
            dataGrid1.TableStyles.Add(ts);

            TextCol = new DataGridColoredTextBoxColumn(de, 3);
            TextCol.MappingName = "dvt";
            TextCol.HeaderText = "ĐVT";
            TextCol.Width = 80;
            ts.GridColumnStyles.Add(TextCol);
            dataGrid1.TableStyles.Add(ts);

            TextCol = new DataGridColoredTextBoxColumn(de, 4);
            TextCol.MappingName = "tennhom";
            TextCol.HeaderText = "Tên nhóm";
            TextCol.Width = 200;
            ts.GridColumnStyles.Add(TextCol);
            dataGrid1.TableStyles.Add(ts);

            TextCol = new DataGridColoredTextBoxColumn(de, 5);
            TextCol.MappingName = "tenhang";
            TextCol.HeaderText = "Tên hãng";
            TextCol.Width = 200;
            ts.GridColumnStyles.Add(TextCol);
            dataGrid1.TableStyles.Add(ts);

            TextCol = new DataGridColoredTextBoxColumn(de, 6);
            TextCol.MappingName = "tennuoc";
            TextCol.HeaderText = "Tên nước";
            TextCol.Width = 200;
            ts.GridColumnStyles.Add(TextCol);
            dataGrid1.TableStyles.Add(ts);
        }
        public Color MyGetColorRowCol(int row, int col)
        {
            Color c = Color.Navy;
            try
            {
                if (row == dataGrid1.CurrentRowIndex)
                {
                    dataGrid1.Select(row);
                    c = Color.White;
                }
            }
            catch
            {
            }
            return c;
        }
        public delegate Color delegateGetColorRowCol(int row, int col);
        public class DataGridColoredTextBoxColumn : DataGridTextBoxColumn
        {
            private delegateGetColorRowCol _getColorRowCol;
            private int _column;
            public DataGridColoredTextBoxColumn(delegateGetColorRowCol getcolorRowCol, int column)
            {
                _getColorRowCol = getcolorRowCol;
                _column = column;
            }
            protected override void Paint(System.Drawing.Graphics g, System.Drawing.Rectangle bounds, System.Windows.Forms.CurrencyManager source, int rowNum, System.Drawing.Brush backBrush, System.Drawing.Brush foreBrush, bool alignToRight)
            {
                try
                {
                    foreBrush = new SolidBrush(_getColorRowCol(rowNum, this._column));
                }
                catch { }
                finally
                {
                    base.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight);
                }
            }
        }
        //private void butMoi_Click(object sender, EventArgs e)
        //{
        //    l_id = 0;
        //    l_iddvt = 0; l_idhc = 0; l_idquycach = 0;
        //    bNew = true;
        //    empty_text();
        //    ena_object(true);
        //    ena_button(false);
        //    tenthuoc.Focus();
        //}
        private void empty_text()
        {
            mathuoc.Text = "";
            tenthuoc.Text = "";
            hamluong.Text = "";
            mahc.Text = "";
            hoatchat.Text = "";
            madvt.Text = "";
            dvt.Text = "";
            madonggoi.Text = "";
            duongdung.Text = "";
            quycach.Text = "";
            manhom.Text = "";
            nhom.Text = "";
            manuoc.Text = "";
            nuocsx.Text = "";
            mahang.Text = "";
            hangsx.Text = "";
            macongty.Text = "";
            congty.Text = "";
        }
        private void ena_object(bool ena)
        {
            tenthuoc.Enabled = ena;
            hamluong.Enabled = ena;
            dvt.Enabled = ena;
            duongdung.Enabled = ena;
            quycach.Enabled = ena;
            hoatchat.Enabled = ena;
            nhom.Enabled = ena;
            hangsx.Enabled = ena;
            nuocsx.Enabled = ena;
            congty.Enabled = ena;
            chkNotuse.Enabled = ena;
        }
        private void ena_button(bool ena)
        {
            butMoi.Enabled = ena;
            butSua.Enabled = ena;
            butLuu.Enabled = !ena;
            butBoqua.Enabled = !ena;
            //butIn.Enabled = ena;
            //butKetthuc.Enabled = ena;
        }

        //private void butSua_Click(object sender, EventArgs e)
        //{
        //    if (l_id == 0)
        //    {
        //        MessageBox.Show("Chọn tên thuốc cần sửa!");
        //        return;
        //    }
        //    bNew = false;
        //    ena_object(true);
        //    ena_button(false);
        //}

        //private void butBoqua_Click(object sender, EventArgs e)
        //{
        //    l_id = 0;
        //    bNew = false;
        //    ena_object(false);
        //    ena_button(true);
        //    reft_text();
        //}
        private void reft_text()
        {
            try
            {
                l_id = long.Parse(dataGrid1[dataGrid1.CurrentCell.RowNumber, 0].ToString());                
            }
            catch { l_id = 0; }
            load_detail();
            ena_object(false);
        }
        private void dataGrid1_CurrentCellChanged(object sender, EventArgs e)
        {
            reft_text();
        }

        private void listhoatchat_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter && mahc.Text != "")
            {
                try
                {
                    DataRow r = m.getrowbyid(dthc, "id=" + int.Parse(mahc.Text));
                    if (r != null)
                    {
                        mahc.Text = r["mahc"].ToString();
                        hoatchat.Text = r["tenhc"].ToString();
                    }
                    listhoatchat.Hide();
                    nhom.Focus();
                }
                catch { }
            }
        }

        //private void butHuy_Click(object sender, EventArgs e)
        //{
        //    if (mathuoc.Text != "" && tenthuoc.Text != "")
        //    {
        //        if (kiemtra(l_id))
        //        {
        //            MessageBox.Show("Danh mục đã sử dụng, không được huỷ!", DAL.AccessData.Msg); return;
        //        }
        //        if (MessageBox.Show("Bạn có đồng ý huỷ không?", DAL.AccessData.Msg, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
        //        {
        //            m.execute_data("delete from " + m.user + ".d_dmbd where id=" + l_id);
        //            load_grid();
        //            reft_text();
        //        }
        //    }
        //}
        private bool kiemtra(long ma)
        {
            if (m.get_data("select * from " + m.user + ".d_donthuocct_"+s_mmyy+" where mabd = " + ma).Tables[0].Rows.Count > 0)
            {
                return true;
            }
            return false;
        }

        private void butChonhc_Click(object sender, EventArgs e)
        {
            frmDmnhom f = new frmDmnhom(m, "d_dmhoatchat", "mahc", "tenhc", "Mã HC", "Hoạt chất", 6, "Danh mục hoạt chất", "mahc");
            //f.ShowDialog();
            load_hoatchat();
        }

        private void txtTimkiem_MouseDown(object sender, MouseEventArgs e)
        {
            if (txtTimkiem.Text.Trim() == "Tìm kiếm")
            {
                txtTimkiem.Text = "";
            }
        }

        private void txtTimkiem_TextChanged(object sender, EventArgs e)
        {
            try
            {
                if (txtTimkiem.Text.Trim() != "Tìm kiếm")
                {
                    CurrencyManager cm = (CurrencyManager)BindingContext[dataGrid1.DataSource, dataGrid1.DataMember];
                    DataView dv = (DataView)cm.List;
                    dv.RowFilter = "ten like '%" + txtTimkiem.Text.Trim() + "%' or tenhc like '%" + txtTimkiem.Text.Trim() + "%' or ma like '%" + txtTimkiem.Text.Trim() + "%'";
                }
            }
            catch { }
        }

        private void butMoi_Click(object sender, EventArgs e)
        {
            l_id = 0;
            l_iddvt = 0; l_idhc = 0; l_idquycach = 0;
            bNew = true;
            empty_text();
            ena_object(true);
            ena_button(false);
            tenthuoc.Focus();
        }

        private void butLuu_Click(object sender, EventArgs e)
        {
            if (!kiemtra()) return;
            if (mathuoc.Text == "") mathuoc.Text = m.getMabd("d_dmbd", tenthuoc.Text, int.Parse(manhom.Text));
            l_id = (bNew) ? m.get_capid_dmbd : l_id;
            if (bNew)
            {
                if (madvt.Text.Trim() == "")
                {
                    l_iddvt = m.get_capid_dmdvt;
                    m.upd_d_cachdung_dvt_quycach(l_iddvt, dvt.Text, "d_dmdvt");
                }
                if (madonggoi.Text.Trim() == "")
                {
                    l_idquycach = m.get_capid_dmquycach;
                    m.upd_d_cachdung_dvt_quycach(l_idquycach, quycach.Text, "d_dmquycach");
                }
                if (mahc.Text.Trim() == "")
                {
                    l_idhc = m.get_capid_dmhoatchat;
                    mahc.Text = m.getMahc(hoatchat.Text);
                    m.upd_d_hoatchat(l_idhc, mahc.Text, hoatchat.Text, chkNotuse.Checked ? 1 : 0);
                }
            }
            m.upd_d_dmbd(l_id, mathuoc.Text, tenthuoc.Text, dvt.Text, hamluong.Text, quycach.Text, duongdung.Text, int.Parse(manhom.Text), int.Parse(mahang.Text), int.Parse(manuoc.Text), hoatchat.Text, mahc.Text, int.Parse(macongty.Text), i_userid, (chkNotuse.Checked ? 1 : 0));
            load_grid();
            reft_text();
            ena_button(true);
            ena_object(false);
            bNew = false;
        }

        private void butSua_Click(object sender, EventArgs e)
        {
            if (l_id == 0)
            {
                MessageBox.Show("Chọn tên thuốc cần sửa!");
                return;
            }
            bNew = false;
            ena_object(true);
            ena_button(false);
        }

        private void butBoqua_Click(object sender, EventArgs e)
        {
            l_id = 0;
            bNew = false;
            ena_object(false);
            ena_button(true);
            reft_text();
        }

        private void butHuy_Click(object sender, EventArgs e)
        {
            if (mathuoc.Text != "" && tenthuoc.Text != "")
            {
                if (kiemtra(l_id))
                {
                    MessageBox.Show("Danh mục đã sử dụng, không được huỷ!", DAL.AccessData.Msg); return;
                }
                if (MessageBox.Show("Bạn có đồng ý huỷ không?", DAL.AccessData.Msg, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
                {
                    m.execute_data("delete from " + m.user + ".d_dmbd where id=" + l_id);
                    load_grid();
                    reft_text();
                }
            }
        }

        private void txtTimkiem_Validated(object sender, EventArgs e)
        {

        }
    }
}